package com.retail.core.util;

import okhttp3.*;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

public class OkHttpUtil {

    public static MediaType JSON = MediaType.get("application/json");


    public static String post(String url, String json) throws IOException {
        RequestBody body = RequestBody.create(JSON, json);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        OkHttpClient client = new OkHttpClient();
        try (Response response = client.newCall(request).execute()) {
            return response.body().string();
        }
    }

    public static void main(String[] args) throws IOException {


        final int CONNECT_TIMEOUT = 30;
        final int READ_TIMEOUT = 30;
        final int WRITE_TIMEOUT = 30;
        OkHttpClient client = new OkHttpClient().newBuilder().build();
        RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("account_id", "7307823849070921740")
                .addFormDataPart("page", "1")
                .addFormDataPart("size", "10")
                .build();
        // 创建OkHttpClient对象
        Request request = new Request.Builder()
                .url("https://open.douyin.com/goodlife/v1/shop/poi/query/")
                .addHeader("access_token", "clt.e13749f66f930df2317c157ebadf06f2Rq3HGJSHnx4aR1uO14fIdStsmzQU_lf")
                .get()
//                .method("POST", body)
                .build();

        // 创建Request对象
//        Request request = new Request.Builder()
//                .url("https://open.douyin.com/goodlife/v1/shop/poi/query?account_id=7307823849070921740&page=1&size=10")
//                .addHeader("access_token", "clt.e13749f66f930df2317c157ebadf06f2Rq3HGJSHnx4aR1uO14fIdStsmzQU_lf")
////                .addHeader("Content-Type", "application/json")
//                .get()
//                .build();

        // 得到Response对象
        Response response = client.newCall(request).execute();

        if (response.isSuccessful()) {
            System.out.println("获取响应状态: " + response.code());
            System.out.println("获取响应信息: " + response.message());
            System.out.println("获取网页源码: " + response.body().string());
        }

        // 清除并关闭线程池
        client.dispatcher().executorService().shutdown();
        // 清除并关闭连接池
        client.connectionPool().evictAll();


//        get("111");
    }

    public static String get(String url) throws IOException {
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, "");
        Request request = new Request.Builder()
                .url("https://open.douyin.com/goodlife/v1/shop/poi/query?account_id=7307823849070921740&page=1&size=10")
                .addHeader("access_token", "clt.e13749f66f930df2317c157ebadf06f2Rq3HGJSHnx4aR1uO14fIdStsmzQU_lf")
                .addHeader("Content-Type", "application/json")
                .addHeader("Cookie", "passport_csrf_token=30dd4d34aee1498b30bd387f405e20eb; passport_csrf_token_default=30dd4d34aee1498b30bd387f405e20eb")
                .build();
        Response response = client.newCall(request).execute();
        System.out.println(response.body().string());
        return "";
    }
}
